--
-- Author: Your Name
-- Date: 2014-11-03 18:00:55
--
local MessageBox = class("MessageBox", require("app.support.view.BaseLayer"))

function MessageBox:ctor(pTarget, cellFun, textBtn, text1, text2)

	CCLuaLog("===MessageBox Onloaded===")

	self.super.ctor(self, pTarget)

	self.mMainView = CCBLayer:load(PathManager:getPopCCBIFile("MessageBox"), "MessageBox")

	tolua.cast(self.mMainView, "CCLayer"):addTo(self)

	self:setTouchEnabled(true)

	self.mPopLayer = tolua.cast(self.mMainView:getChildByName("mPopLayer"), "CCNode")
	self.mBtns   = {}
	self.cellFun = cellFun

	self.mBtn      = tolua.cast(self.mMainView:getChildByName("mBtn"), "CCSprite")
	self.mTitleBox = tolua.cast(self.mMainView:getChildByName("mTitleBox"), "CCScale9Sprite")
	self.mTxt = tolua.cast(self.mMainView:getChildByName("mTxt"), "CCLabelTTF")
	self.mTxt:setString(text2)

	self:addTouchEventListener(self.mBtn, handler(self, self.onBtnTouch))

	local btnTxt = self:newTTFLabelWithOutline(textBtn, text.color1, text.color20, 25, true)

	btnTxt:setPosition(self.mBtn:getContentSize().width / 2, self.mBtn:getContentSize().height / 2)
	btnTxt:addTo(self.mBtn)

	local titleTxt = self:newTTFLabelWithOutline(text1, text.color1, text.color20, 20, true)

	titleTxt:setPosition(self.mTitleBox:getContentSize().width / 2, self.mTitleBox:getContentSize().height / 2)
	titleTxt:addTo(self.mTitleBox)
	titleTxt:setOutlineOpacity(255 * 0.6)

	self:addTouchEventListener(self.mMainView:getChildByName("mCloseBtn"), handler(self, self.onCloseBtnTouch))
	self:addTouchEventListener(self, handler(self, self.onSelfTouch))

end

function MessageBox:hide()

	CCLuaLog("===MessageBox hide===")

	local scaleBack     = CCScaleTo:create(0.2, 0)
	local scaleBackEase = CCEaseBackIn:create(scaleBack)

	transition.execute(self.mMainView:getChildByName("mPopBox"), scaleBackEase,{onComplete = handler(self, self.super.hide)})

end

function MessageBox:onCloseBtnTouch(obj, event)

	if event.name == "began" then

		CCLuaLog("onCloseBtnTouchBegan")

		obj:setFlipY(true)

	elseif event.name == "ended" then

		CCLuaLog("onCloseBtnTouchEnded")

		--self:removeTouchEventListener(self.mMainView:getChildByName("mCloseBtn"))

		local pPoint = CCPoint(event.x, event.y)

		obj:setFlipY(false)

		if self:isContainsPoint(obj, pPoint) then

			CCLuaLog("onScrollBtnTouchAction")

			SoundManager:playMainSceneSound("button")

			self:hide()

		end

	end

end

function MessageBox:onBtnTouch(obj, event)

	if event.name == "began" then

		CCLuaLog("onCloseBtnTouchBegan")

		obj:setFlipY(true)

	elseif event.name == "ended" then

		CCLuaLog("onCloseBtnTouchEnded")

		--self:removeTouchEventListener(self.mMainView:getChildByName("mCloseBtn"))

		local pPoint = CCPoint(event.x, event.y)

		obj:setFlipY(false)

		if self:isContainsPoint(obj, pPoint) then

			CCLuaLog("onScrollBtnTouchAction")

			SoundManager:playMainSceneSound("button")

			if self.cellFun then 
				self.cellFun()
			end

			self:hide()

		end

	end

end

function MessageBox:onSelfTouch(obj, event)

	if event.name == "began" then

		local pPoint = CCPoint(event.x, event.y)

		if self:isContainsPoint(self.mMainView:getChildByName("mPopBox"), pPoint) == false then

			self:hide()

		end

	end

end

return MessageBox